package org.lemon.cla.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage; // 导入 IPage
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; // 导入 Page
import org.lemon.cla.pojo.Book;
import org.lemon.cla.mapper.BookMapper;
import org.lemon.cla.service.IBookService;
import org.lemon.cla.dto.request.BookSearchRequest; // 导入查询请求DTO
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    // 实现模糊查询方法
    @Override
    public IPage<Book> searchBooksByConditions(Page<Book> page, BookSearchRequest request) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();

        // 1. ISBN 模糊查询
        if (request.getIsbn() != null && !request.getIsbn().isEmpty()) {
            queryWrapper.like("isbn", request.getIsbn());
        }

        // 2. 标题模糊查询
        if (request.getTitle() != null && !request.getTitle().isEmpty()) {
            queryWrapper.like("title", request.getTitle());
        }

        // 3. 状态精确匹配
        if (request.getStatus() != null) {
            queryWrapper.eq("status", request.getStatus());
        }

        // 4. 出版社模糊查询
        if (request.getPublisher() != null && !request.getPublisher().isEmpty()) {
            queryWrapper.like("publisher", request.getPublisher());
        }

        // 5. 作者模糊查询
        if (request.getAuthor() != null && !request.getAuthor().isEmpty()) {
            queryWrapper.like("author", request.getAuthor());
        }

        // 6. 存放位置模糊查询
        if (request.getLocation() != null && !request.getLocation().isEmpty()) {
            queryWrapper.like("location", request.getLocation());
        }

        // 可以添加默认排序
        queryWrapper.orderByDesc("publish_date"); // 示例：按出版日期降序

        // 调用 baseMapper.selectPage 方法进行分页查询
        return baseMapper.selectPage(page, queryWrapper);
    }
}